package com.ymatou.iapi.shoppingcart.testcase;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcart.parameter.ChangeCatalogBean;
import com.ymatou.iapi.shoppingcart.service.ChangeCatalogCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerAppShoppingCartService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ShoppingCartMongoWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_ChangeCatalog {
	
	private static ChangeCatalogBean changeCatalogBean;
	private static ChangeCatalogCall changeCatalogCall;
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新购物车商品规格");
	}

	@Before
	public void caseUp() throws Exception {
		changeCatalogBean = new ChangeCatalogBean();
		changeCatalogCall = new ChangeCatalogCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changeCatalogCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_ChangeCatalog_001() {
		 Logger.start(true,"判断接口status是否为200");
		 try {
			 String productId = SellerProductCallService.addMultiCatalogsProduct(sellerId, 
					 sellerName).getJSONObject("Data").getString("ProductID");
			
			 Thread.sleep(60000);
			 
			 BuyerAppShoppingCartService service = new BuyerAppShoppingCartService();
			 service.addShoppingCart(productId, 4085, 1);
			 
			 ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
			 
			 String scId = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', "
			 		+ "'userid':" + 4085 + "}").get("_id").toString();
			 
			 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
			 JSONArray catalogList = new JSONArray(productDetailModelWapper.getProductId(productId).get("CatalogList").toString());
				
			 String catalogId = new JSONObject(catalogList.get(1).toString()).getString("CatalogId");
			 
			 changeCatalogBean.setCatalogId(catalogId);
			 changeCatalogBean.setShoppingCartId(scId);
			 changeCatalogBean.setUserId(4085);
			 
			 changeCatalogCall.setData(changeCatalogBean);
			 changeCatalogCall.callService();
			 
			 int status = changeCatalogCall.getStatus();
			 
			 Logger.comment("changeCatalogCall send:" + changeCatalogCall.getEntityString());
			 Logger.comment("changeCatalogCall response:" + changeCatalogCall.getReturnData());
			 
			 Logger.verifyEquals(200, status, "判断接口status是否为200");		
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_ChangeCatalog_002() {
		 Logger.start(true,"验证result是否为空");
		 try {
			 String productId = SellerProductCallService.addMultiCatalogsProduct(sellerId, 
					 sellerName).getJSONObject("Data").getString("ProductID");
			
			 Thread.sleep(60000);
			 
			 BuyerAppShoppingCartService service = new BuyerAppShoppingCartService();
			 service.addShoppingCart(productId, 4085, 1);
			 
			 ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
			 
			 String scId = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', "
			 		+ "'userid':" + 4085 + "}").get("_id").toString();
			 
			 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
			 JSONArray catalogList = new JSONArray(productDetailModelWapper.getProductId(productId).get("CatalogList").toString());
				
			 String catalogId = new JSONObject(catalogList.get(1).toString()).getString("CatalogId");
			 
			 changeCatalogBean.setCatalogId(catalogId);
			 changeCatalogBean.setShoppingCartId(scId);
			 changeCatalogBean.setUserId(4085);
			 
			 changeCatalogCall.setData(changeCatalogBean);
			 changeCatalogCall.callService();
			 
			 Logger.comment("changeCatalogCall send:" + changeCatalogCall.getEntityString());
			 Logger.comment("changeCatalogCall response:" + changeCatalogCall.getReturnData());
			 
			 Logger.verifyNotNull(changeCatalogCall.getResult(), "验证result是否为空");
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_ChangeCatalog_003() {
		 Logger.start(true,"验证更换购物车商品规格是否成功");
		 try {
			 String productId = SellerProductCallService.addMultiCatalogsProduct(sellerId, 
					 sellerName).getJSONObject("Data").getString("ProductID");
			
			 Thread.sleep(60000);
			 
			 BuyerAppShoppingCartService service = new BuyerAppShoppingCartService();
			 service.addShoppingCart(productId, 4085, 1);
			 
			 ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
			 
			 String scId = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', "
			 		+ "'userid':" + 4085 + "}").get("_id").toString();
			 
			 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
			 JSONArray catalogList = new JSONArray(productDetailModelWapper.getProductId(productId).get("CatalogList").toString());
				
			 String catalogId = new JSONObject(catalogList.get(1).toString()).getString("CatalogId");
			 
			 changeCatalogBean.setCatalogId(catalogId);
			 changeCatalogBean.setShoppingCartId(scId);
			 changeCatalogBean.setUserId(4085);
			 
			 changeCatalogCall.setData(changeCatalogBean);
			 changeCatalogCall.callService();
			 
			 String shoppingCart_catalogId = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', "
				 		+ "'userid':" + 4085 + "}").get("catalogid").toString();
			 
			 Logger.comment("changeCatalogCall send:" + changeCatalogCall.getEntityString());
			 Logger.comment("changeCatalogCall response:" + changeCatalogCall.getReturnData());
			 
			 Logger.verifyEquals(true, changeCatalogCall.getResult().getBoolean("success"), "验证success是否为true");
			 Logger.verifyEquals(shoppingCart_catalogId, catalogId, "验证catalogId更改是否成功");
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
}
